import { withLoading } from 'utils/dva';
import pathToRegexp from 'path-to-regexp';
import { pageConfig } from 'configs/constants';
// import moment from 'moment';
// import { combineDate } from 'utils/share';
import model from '../model';
import services from '../../services';

const defaultConfig = {
  pagination: {
    ...pageConfig,
    total: 0
  },
  list: []
};
export default model.extend ({
    namespace: 'monitorManage',
    state: {
      dataTemplateId: '',
      config: defaultConfig,
      equipList: [],
      templateList: [],
      typeList: [],
      tempList: []
    },

    subscriptions: {
      setup({ dispatch, history }) {
        // 监听路由的变化，请求页面数据
        return history.listen(({ pathname }) => {
          const params = pathToRegexp('/chart/monitorManage').exec(pathname);
          const deviceNo = (params && params[1]) || '';
          dispatch({ type: 'updateState', payload: { deviceNo } });
          // dispatch({ type: 'getList' });
          dispatch({ type: 'queryTypeSelectList' });
        });
      }
    },

    effects: {
      * getList({ payload }, { call, select, update }) {
        const { config: { pagination }, dataTemplateId } = yield select(({ monitorManage }) => monitorManage);
        const { pageSize, current } = pagination;
        const { data }  = yield call(withLoading(services.chart.queryMonitorFiledList, 'list'), 
        { dataTemplateId, ps: pageSize, pn: current, ...payload });
        yield update({ 
          config: {
            list: data,
            pagination: {
              ...pagination,
              total: data.total
            }
          }
        });
      },
      * getTemplateList({ payload }, { call, select, update }) {
        const { config: { pagination } } = yield select(({ monitorManage }) => monitorManage);
        const { pageSize, current } = pagination;
        const { data } = yield call(withLoading(services.chart.queryDataTemplates), 
        { ps: pageSize, pn: current, ...payload });
        yield update({ tempList: data.data });
      },
      // * getEquipList({ payload }, { call, select, update }) {
      //   const { config: { pagination } } = yield select(({ monitorManage }) => monitorManage);
      //   const { pageSize, current } = pagination;
      //   const { data } = yield call(withLoading(services.equipment.getEquipmentList), 
      //   { ps: pageSize, pn: current, ...payload });
      //   yield update({ equipList: data.data });
      // },
      * queryDataTemplateItems({ payload }, { call, update }) {
        const { data } = yield call(withLoading(services.chart.queryDataTemplateItems), { ...payload });
        yield update({ templateList: data });
      },
      * queryTypeSelectList({ payload }, { call, update }) {
        const { data } = yield call(withLoading(services.chart.queryTypeSelectList), { ...payload });
        yield update({ typeList: data });
      },
      * add({ payload }, { call }) {
        yield call(withLoading(services.chart.createMonitorField, 'add', '新增成功'), { ...payload });
      },
      * edit({ payload }, { call }) {
        yield call(withLoading(services.chart.updatemonitorfield, 'add', '编辑成功'), { ...payload });
      },
      * del({ payload }, { call, put }) {
        yield call(withLoading(services.chart.deleteMonitorField, '', '删除成功'), { ...payload });
        yield put({ type: 'getList' });
      }
      
    },

    reducers: {
    }
  });
  